package com.ft.turorial.spring.boot.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ft.turorial.spring.boot.domain.User;
import com.ft.turorial.spring.boot.service.UserService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("user")
public class UserController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value="list", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有用户列表", notes = "无条件获取所有用户列表", code = 200, produces = "application/json")
    public List<User> getUserList(){
        logger.info("从数据库读取User集合");
        // 测试更新数据
        User user = userService.findOne(2);
        user.setName("孙钱");
        logger.info("更新的行数：" + userService.update(user));
        // 测试插入数据
        
        return userService.findAll();
    }
    @RequestMapping(value="{id}", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户", notes = "根据ID获取指定用户", code = 200, produces = "application/json")
    public User getUser(@PathVariable Integer id){
    	return userService.findOne(id);
    }
    
    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public ModelMap jsonTest(HttpServletRequest request,
            HttpServletResponse response) {
        ModelMap map = new ModelMap();
        map.addAttribute("hello", "你好");
        map.addAttribute("veryGood", "很好");
        return map;
    }
}
